package cn.ibizlab.odoo.client.odoo_crm.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import lombok.extern.slf4j.Slf4j;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;

import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import cn.ibizlab.odoo.core.client.model.Icrm_lost_reason;
import cn.ibizlab.odoo.client.odoo_crm.config.odoo_crmClientProperties;
import cn.ibizlab.odoo.core.client.service.Icrm_lost_reasonClientService;
import cn.ibizlab.odoo.client.odoo_crm.model.crm_lost_reasonImpl;
import cn.ibizlab.odoo.client.odoo_crm.feign.crm_lost_reasonFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import cn.ibizlab.odoo.util.feign.FeignRequestInterceptor;
import cn.ibizlab.odoo.util.SearchContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.util.feign.suport.SearchContextFeignEncode;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[crm_lost_reason] 服务对象接口
 */
@Slf4j
@IBIZLog
@Service
public class crm_lost_reasonClientServiceImpl implements Icrm_lost_reasonClientService {

    crm_lost_reasonFeignClient crm_lost_reasonFeignClient;

    @Autowired
    public crm_lost_reasonClientServiceImpl(Decoder decoder, Encoder encoder, Client client, Contract contract ,FeignRequestInterceptor feignRequestInterceptor,odoo_crmClientProperties odoo_crmClientProperties) {
        if (odoo_crmClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.crm_lost_reasonFeignClient = nameBuilder.target(crm_lost_reasonFeignClient.class,"http://"+odoo_crmClientProperties.getServiceId()+"/") ;
		}else if (odoo_crmClientProperties.getServiceUrl()!=null) {
			if (client instanceof LoadBalancerFeignClient) {
				client = ((LoadBalancerFeignClient) client).getDelegate();
			}
			Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.crm_lost_reasonFeignClient = nameBuilder.target(crm_lost_reasonFeignClient.class,odoo_crmClientProperties.getServiceUrl()) ;
		}
    }
    @Override
    public Icrm_lost_reason createModel() {
		return new crm_lost_reasonImpl();
	}


    public void update(Icrm_lost_reason crm_lost_reason){
        Icrm_lost_reason clientModel = crm_lost_reasonFeignClient.update(crm_lost_reason.getId(),(crm_lost_reasonImpl)crm_lost_reason) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), crm_lost_reason.getClass(), false);
        copier.copy(clientModel, crm_lost_reason, null);
    }


    public void removeBatch(List<Icrm_lost_reason> crm_lost_reasons){
        if(crm_lost_reasons!=null){
            List<crm_lost_reasonImpl> list = new ArrayList<crm_lost_reasonImpl>();
            for(Icrm_lost_reason icrm_lost_reason :crm_lost_reasons){
                list.add((crm_lost_reasonImpl)icrm_lost_reason) ;
            }
            crm_lost_reasonFeignClient.removeBatch(list) ;
        }
    }


    public void updateBatch(List<Icrm_lost_reason> crm_lost_reasons){
        if(crm_lost_reasons!=null){
            List<crm_lost_reasonImpl> list = new ArrayList<crm_lost_reasonImpl>();
            for(Icrm_lost_reason icrm_lost_reason :crm_lost_reasons){
                list.add((crm_lost_reasonImpl)icrm_lost_reason) ;
            }
            crm_lost_reasonFeignClient.updateBatch(list) ;
        }
    }


    public void remove(Icrm_lost_reason crm_lost_reason){
        crm_lost_reasonFeignClient.remove(crm_lost_reason.getId()) ;
    }


    public void create(Icrm_lost_reason crm_lost_reason){
        Icrm_lost_reason clientModel = crm_lost_reasonFeignClient.create((crm_lost_reasonImpl)crm_lost_reason) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), crm_lost_reason.getClass(), false);
        copier.copy(clientModel, crm_lost_reason, null);
    }


    public Page<Icrm_lost_reason> fetchDefault(SearchContext context){
        Page<crm_lost_reasonImpl> page = this.crm_lost_reasonFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }


    public void get(Icrm_lost_reason crm_lost_reason){
        Icrm_lost_reason clientModel = crm_lost_reasonFeignClient.get(crm_lost_reason.getId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), crm_lost_reason.getClass(), false);
        copier.copy(clientModel, crm_lost_reason, null);
    }


    public void createBatch(List<Icrm_lost_reason> crm_lost_reasons){
        if(crm_lost_reasons!=null){
            List<crm_lost_reasonImpl> list = new ArrayList<crm_lost_reasonImpl>();
            for(Icrm_lost_reason icrm_lost_reason :crm_lost_reasons){
                list.add((crm_lost_reasonImpl)icrm_lost_reason) ;
            }
            crm_lost_reasonFeignClient.createBatch(list) ;
        }
    }


    public Page<Icrm_lost_reason> select(SearchContext context){
        return null ;
    }


    public void getDraft(Icrm_lost_reason crm_lost_reason){
        Icrm_lost_reason clientModel = crm_lost_reasonFeignClient.getDraft(crm_lost_reason.getId(),(crm_lost_reasonImpl)crm_lost_reason) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), crm_lost_reason.getClass(), false);
        copier.copy(clientModel, crm_lost_reason, null);
    }



}

